import {reqLogin} from '@/api';

//login模块的仓库
const state = {
  storeData: JSON.parse(sessionStorage.getItem("STOREDATA")) || {},
  token: sessionStorage.getItem("GETTOKEN") || '',
  shopId: sessionStorage.getItem("STOREID") || '',
};
const mutations = {
  USERLOGIN(state,storeData) {
    state.storeData = storeData;
    sessionStorage.setItem("STOREDATA", JSON.stringify(storeData));
  }
};
const actions = {
  async userLogin({commit},data){
    let result = await reqLogin(data);
    sessionStorage.setItem("GETTOKEN", result.data.token);
    sessionStorage.setItem("STOREID", result.data.storeId);
    const {storeName,storeUsername,storePhone} = result.data;
    if(result.code==200){
      commit('USERLOGIN',{storeName,storeUsername,storePhone});
      return '成功！'
    }else {
      return Promise.reject(new Error(result.msg));
    }
  },
};
//计算属性
const getters = {

};

//对外暴露store类的一个实例
export default {
  state,
  mutations,
  actions,
  getters,
};